#!/bin/sh 
# 
# processname: keepalived 
# debug : bash -x $0.sh

 # Source function library. 
 . /etc/rc.d/init.d/functions 

  # Source networking configuration. 
  . /etc/sysconfig/network 

   # Check that networking is up. 
   [ "$NETWORKING" = "no" ] && exit 0 

   keepalived="/usr/local/keepalived/sbin/keepalived" 
   prog=$(basename $keepalived) 
   conf_file="/usr/local/keepalived/etc/keepalived/keepalived.conf" 
   lockfile=/var/lock/subsys/keepalived 
   pidfile=/var/run/keepalived.pid
   pidfile_vrrp=/var/run/keepalived_vrrp.pid
   pidfile_check=/var/run/keepalived_check.pid
   procpid=`cat ${pidfile}`

   start() { 
     [ -x $keepalived ] || exit 5 
     [ -f $conf_file ] || exit 6 
     echo -n $"Starting $prog: " 
     $keepalived -D -d -S 0 -f $conf_file -p $pidfile -r $pidfile_vrrp -c $pidfile_check
     retval=$? 
     echo 
     [ $retval -eq 0 ] && touch $lockfile 
     return $retval 
   } 

 stop() { 
   echo -n $"Stopping $prog: " 
   echo "procpid:$procpid"

   [ "${procpid}" == "" ] && exit 0
   kill -9 ${procpid}
   retval=$? 
   echo 
   [ $retval -eq 0 ] && rm -f $lockfile 
   return $retval 
 } 

restart() { 
  configtest || return $? 
  stop 
  start 
} 

reload() { 
  configtest || return $? 
  echo -n $"Reloading $prog: " 
   [ "${procpid}" == "" ] && exit 0
  kill -HUP ${propid}
  RETVAL=$? 
  echo 
} 

force_reload() { 
  restart 
} 

configtest() { 
  $keepalived -t -c $conf_file 
} 

rh_status() { 
  if [ -f ${lockfile} ]; then
    echo "status[1] keepalived is running..."
    return 0
  else
    echo "status[0] keepalived stopped!"
    return 1
  fi
} 

rh_status_q() { 
  rh_status >/dev/null 2>&1 
} 

case "$1" in 
  start) 
    rh_status_q && exit 0 
    $1 
    ;; 
  stop) 
    rh_status_q || exit 0 
    $1 
    ;; 
  restart|configtest) 
    $1 
    ;; 
  reload) 
    rh_status_q || exit 7 
    $1 
    ;; 
  force-reload) 
    force_reload 
    ;; 
  status) 
    rh_status 
    ;; 
  condrestart|try-restart) 
    rh_status_q || exit 0 
    ;; 
  *) 
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
    exit 2 
esac 


